<?php
/**
 * Created by PhpStorm.
 * User: charles
 * Date: 2017-11-10
 * Time: 07:46
 */

namespace App\Http\Controllers\LA;


use App\Helpers\QiniuHelper;
use App\Http\Controllers\Controller;
use App\Models\WechatUpload;
use Carbon\Carbon;
use Dwij\Laraadmin\Models\Module;
use Dwij\Laraadmin\Models\ModuleFields;
use Illuminate\Http\Request;

class WechatUploadController extends Controller
{

    public function index(Request $request)
    {
        $query = WechatUpload::query();
        if (strlen((string) $request->openid)) {
            $query->where('openid', $request->openid);
        }
        $wechatUploads = $query ->with('member')
            ->orderBy('id', 'desc')
            ->paginate(20);
        return view('la.wechat-upload.index', compact('wechatUploads'));
    }

    public function show($id)
    {
        $wechatUpload = WechatUpload::findOrFail($id);
        return view('la.wechat-upload.show', compact('wechatUpload'));
    }

    public function update(Request $request)
    {
        $wechatUpload = WechatUpload::findOrFail($request->id);
        $wechatUpload->fill($request->all());
        $wechatUpload->save();
        return redirect()->back();
    }

    public function destroy($id)
    {
        $wechatUpload = WechatUpload::findOrFail($id);
        QiniuHelper::delete($wechatUpload->key, $wechatUpload->type);
        $wechatUpload->delete();
        return redirect()->back();
    }

    public function onsale($id)
    {
        WechatUpload::query()
            ->where('id', $id)
            ->update(['status' => 0]);
        return redirect()->back();
    }

    public function offsale($id)
    {
        WechatUpload::query()
            ->where('id', $id)
            ->update(['status' => 1]);
        return redirect()->back();
    }

}